# -*- coding: utf-8 -*-  
#process txt file by python
'''
Created on Jan 9, 2015

'''
import os

class PyF():
    '''
    a class analysis a txt file
    '''
    def __init__(self,file_dir):
        self.dir = file_dir
        self.id = 0
        self.data_list = []
        self.base_file = ""
        self.change_file = ""
        
        self._data()
    
    def _data(self):
        self.type = ""
        self.base_line = ""
        self.change_line = ""
        self.base_code = []
        self.change_code = []
        
    
    def open_file(self,file_dir,access_mode = 'r',buffering = -1):
        return open(file_dir,access_mode,buffering)
    
    def read_file(self):
        fp = self.open_file(self.dir)
        return fp.read()
    
    def file_analysis(self):
        '''read file and save in a list'''
        fp = self.open_file(self.dir)
        for eachLine in fp:
            if eachLine[0].isdigit():
                if self.id:               
                    self.data_list.append([self.id,
                                       self.base_line,
                                       self.change_line,
                                       self.base_code,
                                       self.change_code,
                                       self.type])
                
                    self._data()                
                self.id += 1
                
                for i,ch in enumerate(eachLine):
                    if ch.isalpha():
                        self.type = ch
                        self.base_line = eachLine[0:i]
                        self.change_line = eachLine[i+1:-1]
            else:
                if eachLine[0] == "<":
                    self.base_code.append(eachLine[1:-1].strip())
                if eachLine[0] == ">":
                    self.change_code.append(eachLine[1:-1].strip())
                if eachLine[0] == "o":
                    self.data_list.append([self.id,
                               self.base_line,
                               self.change_line,
                               self.base_code,
                               self.change_code,
                               self.type])
                    self.base_file = eachLine.split(":")[1]
                if eachLine[0] == "n":
                    self.change_file = eachLine.split(":")[1]
        
        fp.close()
        return self.data_list,self.base_file,self.change_file
    
def calculate_file_num(folder_path):
    """ Open folder and read all file """
    file_list = os.listdir(folder_path)
    dif_file_list = [result_file for result_file in file_list \
                              if os.path.getsize(folder_path + \
                                              os.path.sep + result_file) != 0]
    total_num = len(file_list)
    dif_file_num = len(dif_file_list)

    return total_num, dif_file_num, dif_file_list
 
if __name__ == "__main__":
    for x in PyF("../TestFile/CompareResult/IdentifierFrequencyBottomLeftResultResult.txt").file_analysis():
        print x
#     print PyF("../HTMLFile/css/table.css").read_file()
#     total, dif, list = calculate_file_num("../TestFile/CompareResult")
#     print total, dif, list
    print 'end'
